package com.example.mychatroom.config;

import com.example.mychatroom.serviceImpl.TestWebSocketAPI;
import com.example.mychatroom.serviceImpl.WebSocketAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import javax.annotation.Resource;



@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private TestWebSocketAPI testWebSocketAPI;

    @Autowired
    private WebSocketAPI webSocketAPI;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 通过这个方法，把刚才创建好的Handler 类给注册到具体的 路径上
        //此时当浏览器，websocket的请求路径是“/test”的时候，就会调用到TestWebSocketAPI 这个类里面的方法
        registry.addHandler(testWebSocketAPI,"/test");
        registry.addHandler(webSocketAPI,"/WebSocketMessage")
                //通过注册这个特定的HttpSession拦截器，就可以把用户给HttpSession中添加的Attribute键值对往WebSocket里也添加一份
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }

}
